//
//  VisitorView.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/3/28.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit

public protocol VisitorViewDelegate: NSObjectProtocol {
    
    func registerBtnDidClick()
    func loginBtnDidClick()
}

class VisitorView: UIView {
    weak var delegate: VisitorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() -> Void {

        addSubview(bgView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        bgView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        maskIconView.xmg_Fill(self)
        homeIconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        messageLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: bgView, size: nil)
        messageLabel.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 240))
        registerBtn.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
        loginBtn.xmg_AlignVertical(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 30), offset: CGPoint(x: 0, y: 20))
    }
    
    func setupVisitorInfo(isHome: Bool, homeIconName: String, messageText: String) -> Void {
        bgView.hidden = !isHome
        homeIconView.image = UIImage(named: homeIconName)
        messageLabel.text = messageText
        if isHome {
            startAnimation()
        }
        
    }
    
    private func startAnimation() -> Void {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 10.0
        animation.removedOnCompletion = false
        bgView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    func registerBtnClick() -> Void {
        delegate?.registerBtnDidClick()
    }
    
    func loginBtnClick() -> Void {
        delegate?.loginBtnDidClick()
    }
    
    // MARK: - 懒加载
    private lazy var homeIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
    private lazy var maskIconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    
    private lazy var bgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人"
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14.0)
        return label
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: #selector(VisitorView.registerBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: #selector(VisitorView.loginBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
